package telco.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.util.Vector;

import telco.client.TelcoSystemInterface;

public class Controller {
	private Model model;
	private TelcoSystemInterface view;
	private GetMessageThread t;
	
	public Controller() {
		model = new Model();
		view = new TelcoSystemInterface();
		
		addListener();
	}
	
	public void setIDs() {
		java.util.Random random = new java.util.Random();
		view.setClientIDFieldText("Client" + (random.nextInt(100) + 1));
		view.setProviderIDFieldText("Group20");
	}
	
	public void showView(){
		view.setVisible(true);
	}

	private void addListener() {
		view.setConnectButtonListener(new connectListener());
		view.setRefreshButtonListener(new refreshListener());
		view.setSendButtonListener(new sendListener());
		view.setChatLineKeyListener(new keyListener());
		view.setCloseListener(new closeListener());
	}
	
	private void sendMessage()
	{
		if(model.getConnected())
        {
    		String selected = view.getDestinationListSelected();
    		
    		if(selected != null)
    		{
    			String msgText = view.getChatLineText();
    			view.printMessage("I wrote to " + selected + ":\n          " + msgText);
    			model.sendMessage(msgText, selected);
    			view.setChatLineText("");
    		}
    		else view.printMessage("Please select a receiver first!");
        }
        else view.printMessage("Please register at provider first!");
	}
	
	class connectListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            
        	model.setMe(view.getClientIDFieldText());
        	
        	if(!model.getConnected())
            {
            	view.printMessage(model.registerAtProvider());
            	
            	t = new GetMessageThread();
            	t.start();
            	
            	view.setClientIDFieldEditable(false);
            	view.setConnectButtonText("Disconnect");
            }
            else 
        	{
            	view.printMessage(model.deregisterAtProvider());
            	view.setClientIDFieldEditable(true);
            	view.setConnectButtonText("Connect");
        	}
        }
	}

	class refreshListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            if(model.getConnected())
            {
		    	Vector<String> clients = model.getAvailableClients();
		        view.setDestinationList(clients);
            }
            else view.printMessage("Please register at provider first!");
        }
	}
	
	class sendListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
        	sendMessage();
        }
	}
	
	class keyListener extends KeyAdapter{
		public void keyPressed(KeyEvent e) { 
			//Sends the Message if ENTER is pressed
			if(e.getKeyChar() == KeyEvent.VK_ENTER) { 
				sendMessage();
			} 
		} 
	}
	
	class closeListener extends WindowAdapter{
		public void windowClosing(WindowEvent e){
			model.deregisterAtProvider();
			System.out.println("Deregistered from Provider!");
			view.dispose();
			System.exit(0);
        }
	}
	
	public class GetMessageThread extends Thread
	{
		@Override
	    public void run()
	    {
			try
			{
				while(model.getConnected())
				{
					Vector<String> messages = model.getMessages();
					if(messages != null)
					{
						for(String s:messages)
						{
							view.printMessage(s);
						}
					}

					//Rufe nur alle 2 Sekunden die Messages vom Provider ab.
					sleep(2000);
				}
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
	    }
	}
}
